//
//  CSZXViewController.m
//  Hxcsfw
//
//  Created by qiysoft on 2017/10/31.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "CSZXViewController.h"
#import "CSZXCell.h"
#define CSZXCellIndentifier @"CSZXCellIndentifier"
#import "IQKeyboardManager.h"
#import "NSString+StrExtension.h"
#import "JGPopView.h"
#define ALERT_TITLE @"提示"
#define SURE @"确定"
#import "BRPickerView.h"
#import "LoginViewController.h"

@interface CSZXViewController ()<UITableViewDataSource,UITableViewDelegate,CSZXCellDelegate,UITextViewDelegate,selectIndexPathDelegate,UIAlertViewDelegate>
{
    CSZXCell *cell1;
}

@property (nonatomic ,strong) NSArray *logTypeArrM;// 分类数组
@property (nonatomic ,strong) NSArray *arrId;// 分类id数组
@property (nonatomic ,strong) NSString *Strtitle; //
@property (nonatomic ,strong) NSString *Strwbcontent; //
@property (nonatomic ,strong) NSString *Strphone; //
@property (nonatomic ,strong) NSString *Strsecrecy; //
@property (nonatomic ,strong) NSString *Strway; //

@end

@implementation CSZXViewController

-(void)FBZX:(UIButton *)button {
    
    if ([cell1.titleTF.text checkInputMsg] || [cell1.contentTF.text checkInputMsg] || [cell1.QXZBtn.titleLabel.text checkInputMsg] || [cell1.phoneTF.text checkInputMsg]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"请完善咨询信息!" delegate:nil cancelButtonTitle:SURE otherButtonTitles:nil];
        [alert show];
        return;
    }else if ([cell1.titleTF.text stringContainsEmoji:cell1.titleTF.text] || [cell1.contentTF.text stringContainsEmoji:cell1.contentTF.text] || [cell1.phoneTF.text stringContainsEmoji:cell1.phoneTF.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"咨询内容暂不支持表情!" delegate:nil cancelButtonTitle:SURE otherButtonTitles:nil];
        [alert show];
        return;
    }
    if ([WShareUtil strNilOrEmpty:cell1.QXZBtn.titleLabel.text] ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"请选择答疑方式!" delegate:nil cancelButtonTitle:SURE otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    if ([cell1.titleTF isFirstResponder]) {
        [cell1.titleTF resignFirstResponder];
    }
    
    _Strtitle = cell1.titleTF.text;
    _Strwbcontent = cell1.contentTF.text;
    _Strsecrecy = @"N";
    _Strphone = cell1.phoneTF.text;
    
    [self FbzxDataRequest];
}

-(void)QXZ:(UIButton *)btn {
    
    /*
    // 尖尖的起始位置
    CGPoint point = CGPointMake(btn.center.x,btn.frame.origin.y+110);
    JGPopView *view2 = [[JGPopView alloc] initWithOrigin:point Width:btn.frame.size.width * 1 Height:40 * 3 Type:JGTypeOfUpCenter Color:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]]; // 尖尖的颜色

    view2.dataArray = _logTypeArrM;
    view2.fontSize = 15;
    view2.row_height = 40;
    view2.titleTextColor = [UIColor blackColor];
    view2.delegate = self;
    [view2 popView];
    */
    [self.view endEditing:YES];

    [BRStringPickerView showStringPickerWithTitle:@"答疑方式" dataSource:@[@"电话",@"文字",@"现场"] defaultSelValue:cell1.QXZBtn.titleLabel.text  resultBlock:^(id selectValue) {

        if ([selectValue isEqualToString:@"电话"]) {
            _Strway = @"phone";
        }else if ([selectValue isEqualToString:@"文字"]) {
            _Strway = @"word";
        }else if ([selectValue isEqualToString:@"现场"]) {
            _Strway = @"scene";
        }
        [cell1.QXZBtn setTitle:selectValue forState:UIControlStateNormal];
        cell1.dyfsLB.text = @"";
    }];
}

// 实现三房库中协议的方法(popCell 行点击事件 获取点击值)
- (void)selectIndexPathRow:(NSInteger)index{
    
    _Strway = [self.arrId objectAtIndex:index];
    
    [cell1.QXZBtn setTitle:[self.logTypeArrM objectAtIndex:index] forState:UIControlStateNormal];
    cell1.dyfsLB.text = @"";
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
    [self.view endEditing:NO];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    
    [self.view endEditing:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"财税咨询";
    
    _logTypeArrM = @[@"电话",@"文字",@"现场"];
    _arrId = @[@"phone",@"word",@"scene"];
    _Strtitle = [NSString string];
    _Strwbcontent = [NSString string];
    _Strsecrecy = [NSString string];
    _Strway = [NSString string];
    _Strphone = [NSString string];
    
    // 1.注册控件
    UINib *cellNib = [UINib nibWithNibName:@"CSZXCell" bundle:nil];
    [self.myTableView registerNib:cellNib forCellReuseIdentifier:CSZXCellIndentifier];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.scrollEnabled = NO;
    self.prototypeCell  = [self.myTableView dequeueReusableCellWithIdentifier:CSZXCellIndentifier];
    
}

#pragma mark - 发布咨询网络请求
- (void)FbzxDataRequest {
    /*
     mode:native,service:fwwb,content:{revert:cszx,username:？,dto:{title:?,wbcontent:?,
     phone:?,secrecy:?,way:?}}
     */
    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"fwwb",@"content":@{@"revert":@"cszx",@"username":[WShareUtil sharedData].dicSession[@"nsrsbh"],@"dto":@{@"title":_Strtitle,@"content":_Strwbcontent,@"phone":_Strphone,@"secrecy":_Strsecrecy,@"way":_Strway}}};
    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};
    
    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
    
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];
        
        if([returnDic[@"resultCode"] isEqualToString:@"SUCCEED"]){
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"您的咨询已提交成功,请等待解答！" delegate:self cancelButtonTitle:SURE otherButtonTitles:nil];
            [alert show];
            return;
            
        }else if([returnDic[@"resultCode"] isEqualToString:@"DTO_ERROR"]){
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"提交数据格式错误！" delegate:nil cancelButtonTitle:SURE otherButtonTitles:nil];
            [alert show];
            return;
            
        }
        
    } failure:^(NSError *error) {
        WLog(@"失败");
    } showProgress:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置UITableViewCell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell1  = [[[NSBundle mainBundle] loadNibNamed:@"CSZXCell" owner:self options:nil] lastObject];
    cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    cell1.delegate = self;
    
    return cell1;
    
}

//行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

//分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//列表行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

//设置行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return WSCREENH_HEIGHT-64;
    
}

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    
    NSUInteger length = [textView.text length];
    NSRange range = NSMakeRange(length - 1, 1);
    if([[textView.text substringWithRange:range] isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    //DLog(@"%lu", (unsigned long)textView.text.length);
    // 该判断用于联想输入, 限制输入字数500
    if (textView.text.length > 499 ) {
        cell1.contentTF.text = [textView.text substringToIndex:500];
    }
    if (cell1.contentTF.text.length == 0) {
        cell1.jtnrLB.hidden = NO;
    }
    else {
        cell1.jtnrLB.hidden = YES;
    }

}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    textView.returnKeyType = UIReturnKeyDefault;//变为搜索按钮UIReturnKeySearch
}

// 点击alertView按钮代理方法-返回前页-（delegate:self可用）
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    [self.navigationController popViewControllerAnimated:YES];

}

@end
